/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Test;

import CRUD.EnterpriseFacadeLocal;
import CRUD.ProductFacadeLocal;
import Entity.BranchEnterprise;
import Entity.Product;
import Entity.ProductPK;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author WinKiQue58
 */
@Stateless
public class EnterpriseUseCase implements EnterpriseUseCaseLocal {
  
    
    @PersistenceContext(unitName = "S3R4_Orcl-ejbPU")
    private EntityManager em;
    
    @EJB private EnterpriseFacadeLocal enterpriseFacade;
    
    @EJB private ProductFacadeLocal productFacade;
    

    //--This Methods are testing functionality of DB  
    @Override
    public String k()
    {
        return ""+enterpriseFacade.count();
    }
    
    @Override
    public List<BranchEnterprise> kk()
    {
        Query q1= em.createQuery
                ("SELECT be FROM Enterprise e, BranchEnterprise be WHERE e.idEnterprise = :ide");
        q1.setParameter("ide", new BigInteger("1"));
        
        List<BranchEnterprise> result=q1.getResultList();
        return result;
    }
    
    @Override
     public String CreateProduct() throws ParseException
    {
        /*DateFormat dateFormat = new SimpleDateFormat("DD/MM/RR HH24:MI:SS,FF TZR");
        Date date = dateFormat.parse("31/05/12 09:26:57,000000000 +05:00");
       long time = date.getTime();
       new Timestamp(time);*/
        
        //Timestamp ts = Timestamp.valueOf("31/05/12 09:26:57,000000000 +05:00");
        Timestamp ts = Timestamp.valueOf("2012-05-12 09:26:57");
        ProductPK ppk=new ProductPK(new BigInteger("3"), new BigInteger("1"), new BigInteger("1"));
        Product p=new Product(ppk, "MC3", new BigInteger("48200.546"), new BigInteger ("15"));
        productFacade.create(p);
        return "success";
    }
    
    public static java.util.Date toDate(java.sql.Timestamp timestamp) {
    long milliseconds = timestamp.getTime() + (timestamp.getNanos() / 1000000);
    return new java.util.Date(milliseconds);
    }

}
